package com.dd.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dd.dao.DdQaDAO;
import com.dd.entity.DdKeyword;
import com.dd.entity.DdQa;
import com.dd.entity.DdShop;
import com.dd.entity.DdSms;
import com.dd.entity.DdUser;
import com.dd.util.DateStrFormat;
import com.dd.util.PubFun;

public class ServerSupportAction extends BaseAction {

	// 客服后台取未回答的问题

	public ActionForward findtoanswer(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		PrintWriter out = response.getWriter();
		DdQaDAO dqd = (DdQaDAO) this.getBean("DdQaDAO");

		try {
			sbf.append("<qas>");
			List list = dqd.findToAnswer();
			for (int i = 0; i < list.size(); i++) {
				DdQa dq = (DdQa) list.get(i);
				sbf.append("<qa id=  />");
			}
			sbf.append("</qas>");
			return null;
		} catch (Exception ne) {
			out.print("");
			out.flush();
			out.close();
			ne.printStackTrace();
			return null;
		}
	}

	public ActionForward edit(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		HttpSession session = request.getSession();
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		PrintWriter out = response.getWriter();
		try {
			if (session.getAttribute("logid") == null) {
				sbf.append("<error>-1</error></root>");
				out.print(sbf.toString());
				return null;
			}
			Integer logid = (Integer) session.getAttribute("logid");
			DdUser du = this.getDdUserDAO().findById(logid);
			this.getUserService().editUser(request, du, sbf);
			out.print(sbf.toString());
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 发短信
	 * 
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward addsms(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		// HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		String logid = String.valueOf((Integer) request.getSession()
				.getAttribute("systemlogid"));// 系统用户是否已经登录

		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		if (logid == null || logid.trim().length() == 0) {// 系统用户尚未登录
			// 返回-1
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
			return null;
		}
		try {
			String phone[] = request.getParameter("phone").split(",");
			String content = request.getParameter("content");
			for (int i = 0; i < phone.length; i++) {
				Long l = Long.parseLong(phone[i]);
				DdSms dss = new DdSms();
				dss.setCreatedate(new Date());
				dss.setStatus(0);
				dss.setPhone(l + "");
				dss.setSendmsg(content);
				this.getDdSmsDAO().saveOrUpdate(dss);
			}
			sbf.append("<sign>1</sign></root>");
			out.print(sbf.toString());
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			e.printStackTrace();
			return null;
		}
		return null;
	}

	/**
	 * 重发短信
	 * 
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward resned(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		// HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		String logid = String.valueOf((Integer) request.getSession()
				.getAttribute("systemlogid"));// 系统用户是否已经登录

		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		if (logid == null || logid.trim().length() == 0) {// 系统用户尚未登录
			// 返回-1
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
			return null;
		}
		try {
			int smsid = Integer.parseInt(request.getParameter("smsid"));// 订单号
			DdSms ds = this.getDdSmsDAO().findById(smsid);
			DdSms dss = new DdSms();
			dss.setCreatedate(new Date());
			dss.setStatus(0);
			dss.setOrderid(ds.getOrderid());
			dss.setOrderstatus(ds.getOrderstatus());
			dss.setPhone(ds.getPhone());
			dss.setSendmsg(ds.getSendmsg());
			dss.setTypes(ds.getTypes());
			this.getDdSmsDAO().saveOrUpdate(dss);
			sbf.append("<sign>1</sign></root>");
			out.print(sbf.toString());
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			e.printStackTrace();
			return null;
		}
		return null;
	}

	/**
	 * 根据orderid取短信发送列表
	 * 
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward querysmsbyorderid(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		// HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		String logid = String.valueOf((Integer) request.getSession()
				.getAttribute("systemlogid"));// 系统用户是否已经登录

		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		if (logid == null || logid.trim().length() == 0) {// 系统用户尚未登录
			// 返回-1
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
			return null;
		}
		if (request.getParameter("orderid") == null
				|| request.getParameter("orderid").equals("")) {// 系统用户尚未登录
			// 返回-1
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			return null;
		}
		try {
			int orderid = Integer.parseInt(request.getParameter("orderid")) - 100000000;// 订单号
			List list = this.getDdSmsDAO().findByOrderid(orderid);

			for (int i = 0; i < list.size(); i++) {

				DdSms ds = (DdSms) list.get(i);
				String type = "发商家";
				if (ds.getTypes().intValue() == 2) {
					type = "发用户";
				}
				String status = "未发送";
				if (ds.getStatus().intValue() == 1) {
					status = "已经提交短信接口";
				} else if (ds.getStatus().intValue() == 2) {
					status = "已经提交运营商";
				} else if (ds.getStatus().intValue() == 3) {
					status = "成功提交运营商";
				} else if (ds.getStatus().intValue() == 4) {
					status = "发送失败";
				}
				sbf.append("<sms id=\"" + ds.getId() + "\" phone=\""
						+ ds.getPhone() + "\" type=\"" + type + "\" status=\""
						+ status + "\" msg=\"" + ds.getSendmsg()
						+ "\" createdate=\""
						+ DateStrFormat.getCurrentTime1(ds.getCreatedate())
						+ "\" />");
			}
			sbf.append("</root>");
			out.print(sbf.toString());
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			e.printStackTrace();
			return null;
		}
		return null;
	}

	/**
	 * 根据keyword添加记录
	 * 
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward setKeyword(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		StringBuffer sbf = new StringBuffer();
		try {
			String keyword = request.getParameter("kw");// 订单号

			out.print(sbf.toString());
		} catch (Exception e) {
			out.print("");
			e.printStackTrace();
			return null;
		}
		return null;
	}

	/**
	 * 根据keyword匹配结果
	 * 
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward getKeyword(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		StringBuffer sbf = new StringBuffer();
		try {

			String kw = request.getParameter("q");// keyword
			if (kw != null && !kw.trim().equals("")) {
				int type = Integer.parseInt(request.getParameter("type"));// keyword
				String kw2 = PubFun.getStringPinYin(kw);
				String kw3 = PubFun.getStringPinYin2(kw);
				List list = this.getDdKeywordDAO().findByKeyword(kw, kw2, kw3,
						type, 10);
				for (int i = 0; i < list.size(); i++) {
					List tmp = (List) list.get(i);
					String str = (String) tmp.get(0);
					Integer num = (Integer) tmp.get(1);
					sbf.append(str + "\n");
				}
			}
			out.print(sbf.toString());
		} catch (Exception e) {
			out.print("");
			e.printStackTrace();
			return null;
		}
		return null;
	}
	
	
}
